
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.Timer;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author fabian
 */
public class TimelinePane extends JPanel {
    Twitter tw;
    Timer tm;
    JTextPane tx = new JTextPane();
    List<Status> statuses;
    ActionListener reload = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           update();
           tm.restart();
        }
    };
    public TimelinePane(Twitter twt, boolean needsUpdate){
        tw = twt;
        tm = new Timer(1200,reload);
        if(needsUpdate) tm.start();
        update();
        this.setSize(750, 500);
        this.setBackground(new java.awt.Color(200,220,255));
        initTextPane();
        this.add(tx);
        this.setVisible(true);
    }
    private void initTextPane(){
        tx.setEditable(false);
        tx.setFocusable(false);
        tx.setSize(750, 500);
        tx.setBackground(new java.awt.Color(200,220,255));
    }
    private void update(){
     String data = "";
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        try {
            statuses =  tw.getFriendsTimeline();
        } catch (TwitterException ex) {} catch (IllegalStateException ex) {}
         for (Status status : statuses) {
                data =  data + "\n\n@" + status.getUser().getScreenName() + " dijo el "+df.format(status.getCreatedAt())+": " + status.getText();
            }
        tx.setText(data);
        tx.moveCaretPosition(0);

    }


}
